package com.etpjava.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.etpjava.action.ManagementAction;
import com.etpjava.dao.ManagementDao;
import com.etpjava.model.Manager;
import com.ibm.db2.jcc.b.v;

public class ManagementDaoImpl implements ManagementDao {
	Log logger=LogFactory.getLog(ManagementDaoImpl.class);
	Configuration cfg=new Configuration().configure();
	SessionFactory sf=cfg.buildSessionFactory();
	Session session=null;
	
	@Override
	public boolean changePassword(Manager mg) {
		try{
			session=sf.openSession();
			session.beginTransaction();
			session.update(mg);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}


	@Override
	public boolean delete(Manager mg) {
		try{
			session=sf.openSession();
			session.beginTransaction();
			//System.out.println(mg);
			session.delete(mg);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean save(Manager mg) {
		try{
			session=sf.openSession();
			session.beginTransaction();
			session.save(mg);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public List<Manager> selectAll() {
		List<Manager> malist=null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			
			String sql="from Manager";
			Query query=session.createQuery(sql);
			
			
			malist =(List<Manager>) query.list();
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			
		}finally{
			session.close();
		}
		return 	malist;
	}
	
	@Override
	public Manager selectID(int id) {
		Manager malist=null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			malist=(Manager)session.get(Manager.class, id);
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			
		}finally{
			session.close();
		}
		return 	malist;
	}
}
